package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.dto.req.JobSkillsQueryRequestDTO;
import com.springboot.hiresystem.dto.req.JobSkillsUpdateRequest;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.dto.resp.JobSkillsResponseDTO;
import com.springboot.hiresystem.domain.JobSkills;
import com.springboot.hiresystem.service.JobSkillsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jobskills")
@Slf4j
public class JobSkillsController {

    private final JobSkillsService jobSkillsService;

    public JobSkillsController(JobSkillsService jobSkillsService) {
        this.jobSkillsService = jobSkillsService;
    }

    // 分页查询
    @GetMapping("/list")
    public Result<PageResponse<JobSkillsResponseDTO>> list(JobSkillsQueryRequestDTO request) {
        QueryWrapper<JobSkills> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(request.getSkillName() != null, "skill_name", request.getSkillName());
        queryWrapper.orderByAsc("skill_id");
        Page<JobSkills> page = new Page<>(request.getPage(), request.getSize());
        Page<JobSkills> resultPage = jobSkillsService.page(page, queryWrapper);

        List<JobSkillsResponseDTO> dtoList = BeanUtil.copyToList(resultPage.getRecords(), JobSkillsResponseDTO.class);

        PageResponse<JobSkillsResponseDTO> response = new PageResponse<>();
        response.setTotal(resultPage.getTotal());
        response.setList(dtoList);

        return Result.success(response);
    }

    // 条件查询
    @PostMapping("/query")
    public Result<List<JobSkillsResponseDTO>> query(@RequestBody JobSkillsQueryRequestDTO request) {
        QueryWrapper<JobSkills> queryWrapper = new QueryWrapper<>();

        if (request.getSkillId() != null) {
            queryWrapper.eq("skill_id", request.getSkillId());
        }
        if (request.getSkillName() != null) {
            queryWrapper.like("skill_name", request.getSkillName());
        }
        queryWrapper.orderByAsc("skill_id");
        List<JobSkills> skills = jobSkillsService.list(queryWrapper);
        List<JobSkillsResponseDTO> dtoList = BeanUtil.copyToList(skills, JobSkillsResponseDTO.class);

        return Result.success(dtoList);
    }

    // 更新或添加
    @PostMapping("/update")
    public Result<String> update(@RequestBody JobSkillsUpdateRequest request) {
        JobSkills jobSkills = BeanUtil.copyProperties(request, JobSkills.class);
        if (jobSkills.getSkillId() !=0){
            jobSkillsService.updateById(jobSkills);
            return Result.success("更新成功");
        }else {
            if (!jobSkillsService.save(jobSkills)) {
                return Result.failure(500, "添加失败");
            }
            return Result.success("新增成功");
        }

    }
//
//    // 添加
//    @PostMapping("/add")
//    public Result<String> add(@RequestBody JobSkills jobSkills) {
//        if (!jobSkillsService.save(jobSkills)) {
//            return Result.failure(500, "添加失败");
//        }
//        return Result.success("添加成功");
//    }

    // 删除
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody JobSkills jobSkills) {
        jobSkillsService.removeById(jobSkills.getSkillId());
        return Result.success("删除成功");
    }
}
